/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.dishes.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyeit.serviceapp.entity.DishesExtend;
import com.qinyeit.serviceapp.repository.dishes.DishesExtendRepository;
import com.qinyeit.serviceapp.service.dishes.DishesExtendService;

/** 
 * ClassName: DishesExtendServiceImpl <br/> 
 * Function: 菜品扩展 业务实现 <br/> 
 * date: 2019-03-01 15:04:09 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class DishesExtendServiceImpl extends BaseService<DishesExtend, Long> implements DishesExtendService {


	@Autowired
	private DishesExtendRepository dishesExtendRepository;


    @Override
    public void saveAll(JSONObject array, Long merchantGroupId) {
        Long storeId = array.getLong("storeId");
        if(storeId==null){
            return;
        }
        dishesExtendRepository.deleteAllByMerchantId(merchantGroupId,storeId);
        DishesExtend extend;
        JSONArray details = array.getJSONArray("details");
        for(int i=0;i<details.size();i++){
            extend = new DishesExtend();
            extend.setName(details.getJSONObject(i).getString("name"));
            extend.setStoreId(storeId);
            extend.setMerchantGroupId(merchantGroupId);
            if(StringUtils.isNotBlank(extend.getName())){
                super.save(extend);
            }
        }
    }
}